package com.chen.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.dto.DishDto;
import com.chen.entity.Dish;
import com.chen.entity.DishFlavor;
import com.chen.exception.StatusException;
import com.chen.mapper.DishMapper;
import com.chen.service.DishFlavorService;
import com.chen.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    public DishMapper dishMapper;
    @Autowired
    public DishFlavorService dishFlavorService;

    @Override
    public Page<DishDto> page1(int page, int pageSize, String name) {
        Page<DishDto> page1 = new Page<>();
        //获取总数据条数
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(name), Dish::getName, name);
        int count = count(lqw);
        //设置总数据条数
        page1.setTotal(count);
        //起始索引
        int starSize = (page - 1) * pageSize;
        if (StringUtils.isNotBlank(name)) {
            //有名字
            List<DishDto> selectpage = this.dishMapper.selectPageWithName(starSize, pageSize, name);
            page1.setRecords(selectpage);
        } else {
            //没有名字
            List<DishDto> selectpage = this.dishMapper.selectPage(starSize, pageSize);
            page1.setRecords(selectpage);
        }
        return page1;
    }

    //@Override
    public void up(long[] ids, int status) {
        for (long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            updateById(dish);
        }
    }

    @Override
    public void up(List<Long> ids, int status) {
        List<Dish> dishes = dishMapper.selectBatchIds(ids);
        for (Dish dish : dishes) {
            dish.setStatus(status);
        }
        updateBatchById(dishes);
    }


    @Override
    public void saveDishAndFlavors(DishDto dishDto) {
        //保存dish
        save(dishDto);
        //保存flavor
        //1获取flavor集合
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            //为每一个flavor保存dish.id
            flavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto findById(long id) {
        //根据id查询菜品信息
        Dish byId = getById(id);
        DishDto dd = new DishDto();
        //根据id查询口味信息
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, id);
        List<DishFlavor> flavors = dishFlavorService.list(lqw);
        //设置口味信息
        BeanUtils.copyProperties(byId, dd);
        //设置口味信息
        dd.setFlavors(flavors);
        return dd;
    }

    @Override
    public void updates(DishDto dishDto) {
        //修改基本菜品数据
        updateById(dishDto);
        //根据菜品id删除旧的口味信息
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(lqw);
        //添加新的口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            //为每一个flavor保存dish.id
            flavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            Dish byId = findById(id);
            if (byId.getStatus() == 1) {
                throw new StatusException();
            }
            removeById(id);
            LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
            lqw.eq(DishFlavor::getDishId, id);
            dishFlavorService.remove(lqw);
        }
    }

    @Override
    public List<DishDto> getList(long categoryId, Integer status) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId, categoryId);
        System.out.println("                     "+status);
        lqw.eq(status != null, Dish::getStatus, status);
        List<Dish> dishList = dishMapper.selectList(lqw);
        List<DishDto> dishDtoList = dishList.stream().map(dish -> {
            DishDto dishDto = new DishDto();
            //将dish对象中的属性转换成dishdto
            BeanUtils.copyProperties(dish, dishDto);
            //通过dishid查询口味信息
            LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
            qw.eq(DishFlavor::getDishId, dish.getId());
            List<DishFlavor> list = dishFlavorService.list(qw);
            //将口味信息存到dishdto
            dishDto.setFlavors(list);
            return dishDto;
        }).collect(Collectors.toList());
        return dishDtoList;
    }
}
